<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><meta name="baidu-site-verification" content="1EB8XoOl0C"><meta name="google-site-verification" content="K7thEgdLm0UfRWJ5MGdF7sCcjClSzAlxFLPv2Oz5CGM"><title> String对象常用方法总结 · 页面仔小杨</title><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="description" content="String对象常用方法总结 - 页面仔小杨"><meta name="keywords" content="js基础 string方法"><meta name="author" content="页面仔小杨"><link rel="short icon" href="/images/favicon.ico"><link rel="stylesheet" href="/css/bubuzou.css"><link rel="search" type="application/opensearchdescription+xml" href="http://itxing666.gitee.io/atom.xml" title="页面仔小杨"><script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script><meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="页面仔小杨" type="application/atom+xml">
</head><body><header><div class="header row"> <a href="/" class="logo-link"><img src="/images/logo.png"></a><ul id="nav_list" class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" data-hover="博文" class="nav-list-link">博文</a></li><li class="nav-list-item"><a href="/archives/" target="_self" data-hover="归档" class="nav-list-link">归档</a></li><li class="nav-list-item"><a href="/about/" target="_self" data-hover="关于" class="nav-list-link">关于</a></li></ul><div class="search"><a id="search_btn" href="#search"></a></div><div id="nav_btn" class="nav-btn"><span></span><span></span><span></span></div></div></header><div class="row scroll-con"><section class="container"><!-- for archive page--><div id="postAr" class="post"><article class="post-block"><h1 class="post-title">String对象常用方法总结</h1><div class="post-info">2020-05-25</div><div class="post-content"><table>
<thead>
<tr>
<th>方法</th>
<th>功能</th>
<th align="left">示例</th>
</tr>
</thead>
<tbody><tr>
<td>charAt()</td>
<td>返回在指定位置的字符</td>
<td align="left">str.charAt(2)</td>
</tr>
<tr>
<td>concat()</td>
<td>连接两个或更多字符串，并返回新的字符串(不改变原字符串)</td>
<td align="left">str.concat(“hello”)</td>
</tr>
<tr>
<td>indexOf()</td>
<td>返回某个指定的字符串值在字符串中首次出现的位置</td>
<td align="left">str.indexOf(“he”)</td>
</tr>
<tr>
<td>includes()</td>
<td>查找字符串中是否包含指定的子字符串(boolean)</td>
<td align="left">str.includes(“he”)</td>
</tr>
<tr>
<td>lastIndexOf()</td>
<td>从后向前搜索字符串，并从起始位置(0)开始计算返回字符串最后出现的位置</td>
<td align="left">str.lastIndexOf(“l”)</td>
</tr>
<tr>
<td>repeat(n)</td>
<td>复制字符串指定次数，并将它们链接在一起返回</td>
<td align="left">str.repeat(2)</td>
</tr>
<tr>
<td>replace()</td>
<td>在字符串中查找匹配的子串，并替换与正则</td>
<td align="left">str.repeat(2)</td>
</tr>
<tr>
<td>search()</td>
<td>查找与正则表达式相匹配的值（开始下标）</td>
<td align="left">str.search(“he”)</td>
</tr>
<tr>
<td>slice(m, n)</td>
<td>提取字符串的片段,返回被提取的部分</td>
<td align="left">str.slice(1, 2)</td>
</tr>
<tr>
<td>split()</td>
<td>把字符串分割为字符串数组, 返回新数组</td>
<td align="left">str.split(“ “)</td>
</tr>
<tr>
<td>substr(m, n)</td>
<td>从m开始提取字符串中指定数目n的字符</td>
<td align="left">str.substr(1, 2)</td>
</tr>
<tr>
<td>substring(m,n)</td>
<td>提取字符串中两个指定的索引之间的字符串（不包含n）</td>
<td align="left">str.substring(1, 2)</td>
</tr>
<tr>
<td>toLowerCase()</td>
<td>把字符串转换小写</td>
<td align="left">str.toLowerCase()</td>
</tr>
<tr>
<td>toUpperCase()</td>
<td>把字符串转换为大写</td>
<td align="left">str.toUpperCase()</td>
</tr>
<tr>
<td>trim()</td>
<td>去除字符串两边的空白</td>
<td align="left">str.trim()</td>
</tr>
</tbody></table>
<a id="more"></a>

<p>注意一下几个方法：</p>
<p>replace()  slice(m, n)  split() </p>
<p>substr(m, n) 与 substring(m,n)的区别</p>
<p><img src="https://raw.githubusercontent.com/itxing666/blogImgs/main/img/bottom.png" alt="bottom"></p>
</div></article></div><div class="right-container"><div class="widget"><div id="arAnchorBar"></div></div></div></section></div><div class="right-menu"></div><div class="modal search-modal"><div class="input-field"><input type="text" id="search_input"><label for="search-input">搜索</label></div><div id="search_result" class="search-result"></div></div><div class="blog-overlay"></div><footer class="row"><div class="footer-con"><div class="paginator"><a href="/2020/05/28/js%E5%9F%BA%E7%A1%80/Array%E5%AF%B9%E8%B1%A1%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95%E6%80%BB%E7%BB%93/" title="Array对象常用方法总结" class="prev">PREV</a><a href="/2020/05/25/%E7%BC%96%E7%A8%8B%E5%B7%A5%E5%85%B7/github%E6%9F%A5%E7%9C%8B%E6%BA%90%E7%A0%81Chrome%E6%8F%92%E4%BB%B6Octotree/" title="github查看源码Chrome插件Octotree" class="next">NEXT</a></div><a href="#comment" class="comment-anchor"></a><div id="vcomments"></div><script>new Valine({
    el: "#vcomments",
    appId: "fTqcCRR2oHhKQGgjSy86Ifkf-gzGzoHsz",
    appKey: "r1WK7286QLlfxnpbWsjs06xH",
    notify: false,
    verify: false,
    avatar: "robohash",
    visitor: true,
    placeholder: "随便说点什么～.～",
});</script><div class="copyright"><p>Copyright © 2016 - 2022  <a target="_blank">页面仔小杨</a> 版权所有【实战干货、原创分享】 <br></p><p> <span style="padding-right: 6px;">关注公众号：页面仔小杨</span></p></div><div class="totop"><i></i></div></div></footer><script async src="https://cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-MML-AM_CHTML"></script><script src="/scripts/jquery-1.8.2.min.js"></script><script src="/scripts/ar-anchor.js"></script><script src="/scripts/main.js"></script><script>(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;e=o.createElement(i);r=o.getElementsByTagName(i)[0];e.src='//www.google-analytics.com/analytics.js';r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));ga('create',"UA-65933410-1",'auto');ga('send','pageview');</script></body></html>